package com.zsrt.jmj.teacher.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zsrt.jmj.teacher.entity.TeacherEntity;
import com.zsrt.jmj.teacher.service.TeacherService;
import com.zsrt.jmj.teacher.service.impl.TeacherServiceImpl;
import com.zsrt.jmj.utils.GsonUtil;


public class TeacherController extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	private TeacherService service= new TeacherServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String method = req.getParameter("method");
    	
    	Map<String, Object> map = new HashMap<>();
    	if(!"".equals(method) && null != method){
    		if(method.equals("add")){
    			String teacherName = req.getParameter("teacherName");
    			String salary = req.getParameter("salary");
    			String positionalId = req.getParameter("positionalId");
    			
    			TeacherEntity entity  =  new TeacherEntity();
    			entity.setTeacherName(teacherName);
    			entity.setSalary(Double.parseDouble(salary));
    			entity.setPositional(Integer.parseInt(positionalId));
				service.add(entity,map);
    		}else if("findAllTeachers".equals(method)){
    			
    			service.findAllTeachers(map);
    		}else if("updateById".equals(method)){
    			
    			String id = req.getParameter("id");
    			String teacherName = req.getParameter("teacherName");
    			String salary = req.getParameter("salary");
    			String positionalId = req.getParameter("positionalId");
    			
    			TeacherEntity entity  =  new TeacherEntity();
    			entity.setId(Integer.parseInt(id));
    			entity.setTeacherName(teacherName);
    			entity.setSalary(Double.parseDouble(salary));
    			entity.setPositional(Integer.parseInt(positionalId));
    			service.updateById(entity,map);
    		}else if("findTeacherById".equals(method)){
    			String id = req.getParameter("id");
    			service.findTeacherById(id,map);
    		}else if("deleteByIds".equals(method)){
    			 String ids = req.getParameter("ids");
    			service.deleteByIds(ids,map);
    		}
    	}
    
    	GsonUtil.printJson(resp, map);
	
	}

}
